<template>
    <div class="edit-container">
      <h1>修改新闻信息</h1>
  
      <!-- 输入新闻ID -->
      <div class="input-container">
        <label for="newsId">请输入新闻ID:</label>
        <input
          type="text"
          id="newsId"
          v-model="newsIdInput"
          placeholder="请输入新闻ID"
          @keyup.enter="fetchNewsInfo"
        />
        <button @click="fetchNewsInfo">加载新闻信息</button>
      </div>
  
      <!-- 新闻信息展示 -->
      <div v-if="newsInfo">
        <form @submit.prevent="updateNews" class="news-form">
          <div v-if="errorMessages.length" class="error">
            <p v-for="(error, index) in errorMessages" :key="index">{{ error }}</p>
          </div>
  
          <div class="form-group">
            <label for="newsTitle">新闻标题</label>
            <input
              type="text"
              id="newsTitle"
              v-model="newsInfo[0].title"
              required
            />
          </div>
  
          <div class="form-group">
            <label for="newsContent">新闻内容</label>
            <textarea
              id="newsContent"
              v-model="newsInfo[0].content"
              required
            ></textarea>
          </div>
  
          <div class="form-group">
            <label for="newsImage">新闻图片URL</label>
            <input
              type="text"
              id="newsImage"
              v-model="newsInfo[0].imageName"
              required
            />
          </div>
  
          <!-- 新增字段 -->
          <div class="form-group">
            <label for="newsAuthor">新闻作者</label>
            <input
              type="text"
              id="newsAuthor"
              v-model="newsInfo[0].author"
              required
            />
          </div>
  
          <div class="form-group">
            <label for="newsPublishDate">新闻出版时间</label>
            <input
              type="date"
              id="newsPublishDate"
              v-model="newsInfo[0].publishdate"
              required
            />
          </div>
  
          <div class="form-group">
            <label for="newsTypeId">新闻类型ID</label>
            <input
              type="number"
              id="newsTypeId"
              v-model="newsInfo[0].typeid"
              required
            />
          </div>
  
          <button type="submit" class="submit-btn">更新新闻</button>
        </form>
      </div>
  
      <!-- 如果新闻信息未加载 -->
      <div v-else-if="newsIdInput && !newsInfo">
        <p>加载新闻信息中...</p>
      </div>
  
      <div v-else>
        <p>请输入新闻ID并点击加载按钮获取新闻信息。</p>
      </div>
    </div>
  </template>
  
  <script setup>
  import { ref } from 'vue';
  import axios from 'axios';
  // 响应式数据
  const newsIdInput = ref('');
  const newsInfo = ref(null);
  const errorMessages = ref([]);
  
  // 获取新闻信息
  const fetchNewsInfo = async () => {
    if (!newsIdInput.value) {
      errorMessages.value = ['新闻ID不能为空'];
      return;
    }
    try {
      const response = await axios.get(`http://localhost:3001/IdNews?id=${newsIdInput.value}`);
      newsInfo.value = response.data;
      errorMessages.value = [];
    } catch (error) {
      console.error('获取商品信息失败:', error);
      errorMessages.value = ['加载商品信息失败，请稍后再试'];
      newsInfo.value = null;
    }
  };
  
  // 更新新闻信息
  const updateNews = async () => {
    if (!newsInfo.value || !newsInfo.value[0].title || !newsInfo.value[0].content || !newsInfo.value[0].imageName || !newsInfo.value[0].author || !newsInfo.value[0].publishdate || !newsInfo.value[0].typeid) {
      errorMessages.value = ['所有字段都必须填写'];
      return;
    }
    try {
      const response = await axios.post('http://localhost:3001/updateNews', {
        id: newsInfo.value[0].newid,
        title: newsInfo.value[0].title,
        content: newsInfo.value[0].content,
        imageName: newsInfo.value[0].imageName,
        author: newsInfo.value[0].author,
        publishdate: newsInfo.value[0].publishdate,
        typeid: newsInfo.value[0].typeid,
      });
  
      if (response.data.status === 200) {
        alert('信息更新成功');
      } else {
        alert('信息更新失败，请稍后再试');
      }
    } catch (error) {
      console.error('更新信息失败:', error);
      alert('更新信息失败，请稍后再试');
    }
  };
  </script>
  
  <style scoped>
  /* 基础样式 */
  body {
    font-family: Arial, sans-serif;
    background-color: #f7f7f7;
    margin: 0;
    padding: 0;
  }
  
  .edit-container {
    width: 80%;
    max-width: 800px;
    margin: 50px auto;
    background-color: #ffffff;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  }
  
  h1 {
    text-align: center;
    color: #333;
    font-size: 24px;
    margin-bottom: 20px;
  }
  
  /* 输入框和按钮 */
  .input-container {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 20px;
  }
  
  .input-container input {
    width: 60%;
    padding: 8px;
    border: 1px solid #ccc;
    border-radius: 4px;
  }
  
  .input-container button {
    padding: 8px 16px;
    background-color: #4CAF50;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
  }
  
  .input-container button:hover {
    background-color: #45a049;
  }
  
  /* 表单样式 */
  .news-form {
    display: flex;
    flex-direction: column;
  }
  
  .form-group {
    margin-bottom: 15px;
  }
  
  .form-group label {
    font-weight: bold;
    margin-bottom: 5px;
    display: block;
    color: #333;
  }
  
  .form-group input,
  .form-group textarea {
    width: 100%;
    padding: 10px;
    border: 1px solid #ccc;
    border-radius: 4px;
    font-size: 14px;
  }
  
  .form-group textarea {
    height: 150px;
    resize: vertical;
  }
  
  button.submit-btn {
    padding: 10px 20px;
    background-color: #007bff;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 16px;
  }
  
  button.submit-btn:hover {
    background-color: #0056b3;
  }
  
  /* 错误提示 */
  .error {
    color: #f44336;
    margin-bottom: 15px;
  }
  
  .error p {
    margin: 5px 0;
  }
  
  /* 加载状态 */
  p {
    font-size: 16px;
    color: #333;
    text-align: center;
  }
  
  /* 默认提示 */
  .edit-container > p {
    text-align: center;
    font-size: 16px;
    color: #888;
  }
  </style>